<?php

namespace app\model;

use think\Model;

class Paymethod extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'paymethod';

    //points_pay  积分支付
    //balance_pay  余额支付
    //wx_pay 微信支付
    //offline_pay  线下支付
    //delivery_pay  delivery_pay
    public static function datainitial()
    {
        $weid = weid();
        $data[0] = [
            'weid' => $weid,
            'code' => 'balance_pay',
            'title' => '余额支付',
            'settings' => '',
            'collection_voucher' => 0,
            'group_ids' => '',
            'status' => 0,
            'sort' => 10,
        ];
        $data[1] = [
            'weid' => $weid,
            'code' => 'wx_pay',
            'title' => '微信支付',
            'settings' => '',
            'collection_voucher' => 0,
            'group_ids' => '',
            'status' => 0,
            'sort' => 20,
        ];
        $data[2] = [
            'weid' => $weid,
            'code' => 'ali_pay',
            'title' => '支付宝',
            'settings' => '',
            'collection_voucher' => 0,
            'group_ids' => '',
            'status' => 0,
            'sort' => 30,
        ];
        $data[3] = [
            'weid' => $weid,
            'code' => 'offline_pay',
            'title' => '线下支付',
            'settings' => '',
            'collection_voucher' => 1,
            'group_ids' => '',
            'status' => 0,
            'sort' => 40,
        ];
        $data[4] = [
            'weid' => $weid,
            'code' => 'delivery_pay',
            'title' => '货到付款',
            'settings' => '',
            'collection_voucher' => 1,
            'group_ids' => '',
            'status' => 0,
            'sort' => 50,
        ];

        foreach ($data as $vo) {
            if (!(self::where(['weid' => $weid, 'code' => $vo['code']])->find())) {
                self::create($vo);
            }
        }
    }

    public static function getwx_settings()
    {
        $data = self::where(['weid' => weid(), 'code' => 'wx_pay'])->order('id desc')->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }

        $data['settings'] = iunserializer($data['settings']);

        return $data['settings'];
    }
    public static function getali_settings()
    {
        $data = self::where(['weid' => weid(), 'code' => 'ali_pay'])->order('id desc')->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }

        $data['settings'] = iunserializer($data['settings']);

        return $data['settings'];
    }

    public static function paysuccess($message)
    {

        //支付成功

        $tid = $message['out_trade_no'];
        $tidarray = explode('-', $tid);

        if (!empty($tidarray[1])) {
            $tid = $tidarray[1];
        }
        if ($tidarray[0] == 'paybill') {

            $data['order_num_alias'] = $tid;
            $data['user_agent'] = serialize($message);
            $data['pay_result'] = serialize($message);
            $data['description'] = '直接付款赠送积分';

            $ordermod = new Order;
            $ordermod->pay_order($data);

        } elseif ($tidarray[0] == 'recharge') {

            Member::where('id', UID())
                ->inc('balance', $message['total_fee'])
                ->update();

            $cashlogsdata['uid'] = UID();
            $cashlogsdata['weid'] = weid();
            $cashlogsdata['id'] = '0';
            $cashlogsdata['order_num_alias'] = '0';
            $cashlogsdata['remarks'] = '充值';
            $cashlogsdata['prefix'] = 1;
            $cashlogsdata['amount'] = $message['total_fee'];

            MemberCashlogs::create($cashlogsdata);
        } elseif ($tidarray[0] == 'additional') {

            Order::itional_pay($tid);
        } elseif ($tidarray[0] == 'tongue') {
            Tongue::where('id', $tid)->update(['is_pay' => 1]);
        } elseif ($tidarray[0] == 'vip') {
            Viporder::pay_order(['order_num_alias' => $tid]);
        } elseif ($tidarray[0] == 'order') {

            $fee = $message['total_fee'];
            if (!empty($fee)) {
                $order = Order::find($tid);
                if (!empty($order)) {
                    $order = $order->toArray();
                }
            }

            $data['order_num_alias'] = $tid;
            $data['user_agent'] = serialize($message);
            $data['pay_result'] = serialize($message);

            $ordermod = new Order;
            $ordermod->pay_order($data);
        }
    }
}
